﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Net;

namespace WeiboTimer
{
    public class Picture
    {
        #region Picture
        public string DownloadPicture(string url)
        {
            //iLogger2.logger("开始下载图片.");
            int n = url.LastIndexOf(".");
            string ext = url.Substring(n, url.Length - n);
            ext = (ext == ".jpeg") ? ".jpg" : ext;
            if (ext != ".jpg" && ext != ".png" && ext != ".gif")
            {
                throw (new Exception("Unsupported Picture Type: " + ext));
            }
            string URLAddress = url.Substring(0, n);
            string filename = "ToBeUploaded" + ext;
            try
            {
                //WebClient client = new WebClient();
                //client.DownloadFile(url, filename);
                DownloadFile(url, filename);
                Thread.Sleep(1000);
            }
            catch (Exception ex)
            {
                throw (new Exception("Download Error:" + ex.Message));
            }
            return filename;
        }
        public static void DownloadFile(string url, string filename)
        {
            WebClient webClient = new WebClient();
            Stream stream = webClient.OpenRead(url);
            byte[] arrByte = new byte[1024];
            long completedByteCount = 0;
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }
            FileStream fStream = new FileStream(filename, FileMode.CreateNew, FileAccess.Write);
            while (true)
            {
                int readCnt = stream.Read(arrByte, 0, 1024);
                if (readCnt == 0) break;
                fStream.Write(arrByte, 0, readCnt);
                completedByteCount += readCnt;
                //double percent = (int)((float)completedByteCount / FileLength * 100);
                //CompletedProseccEventArgs e = new CompletedProseccEventArgs(percent, completedByteCount);
                //OnCompletedProsecc(e);
            }
            stream.Close();
            fStream.Close();
        }
        public byte[] OpenFile(string fileName)
        {
            byte[] imgBuff = null;
            try
            {
                FileInfo imageFile = new FileInfo(fileName);
                using (FileStream stream = imageFile.OpenRead())
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    try
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        imgBuff = reader.ReadBytes((int)stream.Length);
                    }
                    finally
                    {
                        reader.Close();
                        stream.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                imgBuff = null;
                throw (new Exception("OpenFile: " + fileName + ", Error:" + ex.Message));
            }
            return imgBuff;
        }
        #endregion
    }
}
